<?php

namespace app\api\model;

use think\Model;

class UserModel extends Model
{
    protected $name = 'user';
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $append = [
        'video_text',
        'followernum_text',
        'follow_text'
    ];

//    public function getFollowernumTextAttr($value,$data)
//    {
//        // 查询粉丝数
//        return $follower = Db::name('follow')->where('follow_id',$data['id'])->count();
//    }
//
//    public function getVideoTextAttr($value,$data)
//    {
//        // 作品数
//        return $follower = Db::name('video')->where('author_id',$data['id'])->count();
//    }
//
//    public function getFollowTextAttr($value,$data)
//    {
//        $token = Request::instance()->header('token');
//        $uid = Cache::store('redis')->get($token);
//        $user = json_decode($uid,true);
//        $follower = Db::name('follow')->where('follower_id',$user['id'])->where('follow_id',$data['id'])->find();
//        if ($follower){
//            return 1;
//        }else{
//            return 2;
//        }
//    }



    // 获取全部作者
    public function getList()
    {
        return $this->field('id,username,headimg,tel')->where('type',2)->limit(8)->select();
    }

    public function authorvideo()
    {
        return $this->hasMany('VideoModel','author_id','id')->field('id,author_id,video_title,video_img,play_num')->limit(4);
    }
    // 获取详情
    public function getOneAuthor($id)
    {
        return $this->where('id',$id)->field('id,tel,username,headimg,content')->find();
    }

    // 获取用户已关注的作者
    public static function getUserFollow($arr)
    {
        return self::where('id','in',$arr)->field('id,tel,username,headimg')->limit(8)->select();
    }

    // 获取未关注的作者
    public function getFollow($follow)
    {
        return $this->where('id','not in',$follow)->where('type',2)->field('id,tel,username,headimg,content')->limit(8)->select();
    }

    // 随机获取未关注作者
    public function getRandAuthor($follow,$limit)
    {
        return $this->where('id','not in',$follow)->where('type',2)->field('id,tel,username,headimg,content')->orderRaw('rand()')->limit($limit)->select();
    }

    // 判断第三方是否绑定手机
    public static function getUserTel($openid)
    {
        return self::where('openid',$openid)->field('tel')->find();
    }

    // 第三方登录 获取用户主表信息
    public static function getUserInfoBytel($tel)
    {
        return self::where('tel',$tel)->field('id,username,tel,headimg')->find();
    }
}
